package com.designoutput.ui.gallery
{
	import com.designoutput.ui.GalleryCanvas;
	import com.designoutput.ui.ImageHolderDialog;
	import com.designoutput.ui.newControls.ControlBase;
	import com.designoutput.utils.Globals;
	import com.designoutput.vo.GalleryImageVo;
	import com.designoutput.vo.GalleryVo;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * The Gallery body contains all image elements.
	 */	
	public class GalleryGrid extends ControlBase
	{	
		public var parentCanvas:GalleryCanvas;
		private var _createdItems:Object;
		private var _currentGalleryVo:GalleryVo;
		private var _itemWidth:Number = 200;
		private var _itemHeight:Number = 180;
		private var _horizontalGap:Number = 8;
		private var _verticalGap:Number = 8;
		private var _currentPage:int = 0;
		private var _itemsPerPage:int = 0;
		private var _totalPages:int = 0;
		private var _numColumns:int = 0;
		private var _numRows:int = 0;
		private var _itemsHolder:Sprite;
		private var _footer:GalleryFooter;
		
		public function GalleryGrid(parent:GalleryCanvas):void {
			parentCanvas = parent;
			super();
		}
		
		override protected function initialize():void
		{
			super.initialize();
						
			_itemsHolder = new Sprite;
			_itemsHolder.x = 0;
			_itemsHolder.y = 0;
			addChild(_itemsHolder);

			_footer = new GalleryFooter(this);
			addChild(_footer);
		}
		
		protected function get galleryWidth():Number {
			return _width;
		}
		
		protected function get galleryHeight():Number {
			return _height - 60;
		}
		
		override protected function updateDisplay():void {
			super.updateDisplay();
			
			calculateGalleryGrid();
			
			graphics.clear();
			graphics.lineStyle(2, 0, 1);
			graphics.drawRect(0, 0, _width, _height);
			graphics.beginFill(0xD3D6DB, 0.7);
			graphics.drawRect(0, 0, _width, _height);
			graphics.endFill();
			
			_itemsHolder.y = 0;
			_footer.x = 1;
			_footer.y = galleryHeight;
			_footer.width = _width - 2;
			_footer.height = 60;
			
			var rect:Rectangle = _itemsHolder.scrollRect || new Rectangle();
			rect.x = 0;
			rect.y = 0;
			rect.width = galleryWidth;
			rect.height = galleryHeight;
			_itemsHolder.scrollRect = rect;
		}
		
		private function calculateGalleryGrid():void{
			//Update grid aspects
			var itemWidth:Number = _itemWidth + _horizontalGap;
			var itemHeight:Number = _itemHeight + _verticalGap;
			
			var availableWidth:Number = galleryWidth - 10 + 8; //-10 of gallery margins, +8 to compensate last gap considered on itemWidth
			var availableHeight:Number = galleryHeight - 10 + 8; //-10 of gallery margins, +8 to compensate last gap considered on itemHeight
			
			_numColumns = Math.floor(availableWidth / itemWidth);
			if(_numColumns <= 0)
				_numColumns = 1;
			
			_numRows = Math.floor(availableHeight / itemHeight);
			if(_numRows <= 0)
				_numRows = 1;
			
			_itemsPerPage = _numColumns * _numRows;
			//
			
			trace("calculateGalleryGrid");
			calculateGalleryPages();			
			updatePage();
			updateFooter();
		}
		
		private function calculateGalleryPages():void {
			if(_currentGalleryVo == null){
				_currentPage = 0;
				_totalPages = 0;
				return;
			}
			
			_totalPages = Math.ceil(_currentGalleryVo.images.length / _itemsPerPage);
					
			if(_currentPage > _totalPages - 1)
				_currentPage = _totalPages - 1;
		}
		
		public function setGalleryVo(vo:GalleryVo):void{
			if(_currentGalleryVo != vo){
				_createdItems = {};
				_currentPage = 0;
				_currentGalleryVo = vo;
			}
			trace("setGalleryVo");
			calculateGalleryPages();
			updatePage();
			updateFooter();
		}
		
		private function updatePage():void{
			while(this._itemsHolder.numChildren)
				this._itemsHolder.removeChildAt(0);
									
			if(_currentGalleryVo == null)
				return;
			
			var curX:Number = 5;
			var curY:Number = 5;
			
			var firstItem:int = _currentPage * _itemsPerPage;
			var lastItem:int = firstItem + _itemsPerPage;
			for(var i:int = firstItem; i < Math.min(lastItem, _currentGalleryVo.images.length); i++){
				var item:ImageItem = _createdItems[String(i)];				
				if(item == null){
					var imageItemVo:GalleryImageVo = _currentGalleryVo.images[i];			
					
					item = new ImageItem();
					item.addEventListener(MouseEvent.CLICK, item_clickHandler, false, 0, true);
					
					item.imageVo = imageItemVo;
										
					_createdItems[String(i)] = item;
				}
				
				_itemsHolder.addChild(item);
				
				var column:int = i % _numColumns;
				var row:int = i / _numColumns;
				
				item.x = curX;
				item.y = curY;
				item.width = _itemWidth;
				item.height = _itemHeight;
				
				if(column == _numColumns - 1){
					curX = 5;
					curY += _itemHeight + _verticalGap;
				} else {
					curX += _itemWidth + _horizontalGap;
				}
			}
		}
		
		private function item_clickHandler(e:MouseEvent):void
		{		
			var imgItem:ImageItem = (e.currentTarget as ImageItem);
			
			if(e.ctrlKey){				
				var imageHolderDialog:ImageHolderDialog = new ImageHolderDialog();				 
				imageHolderDialog.parentCanvas = parentCanvas;
				imageHolderDialog.setImageDimension(imgItem.imageBody.height, imgItem.imageBody.width);				
				imageHolderDialog.show(true);
				imageHolderDialog.imageVo = imgItem.imageVo;
			} else {
				parentCanvas.onChangeImage(imgItem.imageVo);		
			}				
		}
		
		private function gotoPage(page:int):void {
			if(page < 0)
				page = 0;
			
			if(page > _totalPages - 1)
				page = _totalPages - 1;
			
			_currentPage = page;
			
			updatePage();
			updateFooter();
		}
		
		public function previousPage():void{
			gotoPage(_currentPage - 1);
		}
		
		public function nextPage():void{
			gotoPage(_currentPage + 1);
		}
		
		private function updateFooter():void{
			if(_totalPages == 0) {
				_footer.visible = false;
				return;
			} else {
				_footer.visible = true;
			}
			
			var currentPosition:int = _currentPage * _itemsPerPage;
			
			var txt:String = Math.min(_currentGalleryVo.images.length, currentPosition + 1)
				+ "-" + Math.min(_currentGalleryVo.images.length, (currentPosition + _itemsPerPage)) + " of " + _currentGalleryVo.images.length;
			
			_footer.setPageInfor(txt);
			
			_footer.showNextBtn(_currentPage < _totalPages - 1);
			_footer.showPreviousBtn(_currentPage > 0);
		}
	}
}